package controller;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.util.JRLoader;

import persistence.TermosDao;
import persistence.TermosDaoImpl;

import model.Glossario;
import model.Login;

@WebServlet("/consultar")
public class ConsultarCadastradosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ConsultarCadastradosServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String erro = "";
		Login l = new Login();
		l.setLogin("admin");
		int codigoGrupo = Integer.parseInt(request.getParameter("listagrupos")
				.toString());
		TermosDao tDao = new TermosDaoImpl();

		ServletContext context = getServletContext();
		
		String dirTeste = context.getRealPath("/WEB-INF/reports");
		File teste = new File(dirTeste,"rel.jasper");
		if (teste.exists()){
			System.out.println(teste.getAbsolutePath());
		}
		
		ResultSet termosCadastrados;
		byte[] bytes = null;
		
		try {
			termosCadastrados = tDao.listaTermosGrupo(codigoGrupo);
//			String jasper = this
//					.getClass()
//					.getResource("/files/rel.jasper")
//					.getFile()
//					.substring(
//							1,
//							this.getClass().getResource("/files/rel.jasper")
//									.getFile().length());
			String pdf = context.getRealPath("/WEB-INF/pdfs/")+File.separator+String.valueOf(codigoGrupo)+".pdf";
			
			File arquivo = new File(pdf);
			if (arquivo.exists()){
				arquivo.delete();
			}
			JRResultSetDataSource jrDS = new JRResultSetDataSource(
					termosCadastrados);
			String jasper = context.getRealPath("/WEB-INF/reports/rel.jasper");
			JasperPrint print = JasperFillManager.fillReport(jasper,
					 new HashMap(), jrDS);
			JasperExportManager.exportReportToPdfFile(print, pdf);
//			 JasperPrint print = JasperFillManager.fillReport(jasper,
			// new HashMap(), jrDS);
			bytes = JasperExportManager.exportReportToPdf(print);
//			JasperReport relatorioJasper = (JasperReport) JRLoader
//					.loadObjectFromFile(context
//							.getRealPath("/WEB-INF/reports/rel.jasper"));
			// Transfoma o report em conjunto de bytes
//			bytes = JasperRunManager.runReportToPdf(relatorioJasper,
//					new HashMap(), jrDS);
		} catch (SQLException | JRException e) {
			erro = e.getMessage();
		}

		if (bytes != null && bytes.length > 0) {
			response.setContentType("application/pdf");
			response.setContentLength(bytes.length);
			ServletOutputStream outputStream = response.getOutputStream();
			outputStream.write(bytes, 0, bytes.length);
			outputStream.flush();
			outputStream.close();
		} 

		// request.setAttribute("erro", erro);
		// request.setAttribute("login", l);
		// request.setAttribute("termosCadastrados", termosCadastrados);
		// RequestDispatcher view = request.getRequestDispatcher("admin.jsp");
		// view.forward(request, response);

	}

}
